package com.final_不可修改;

public class final练习 {
    public static void main(String[] args) {
//        计算圆形面积，用三种定义方式
        Area1 p1 = new Area1(10);
        System.out.println(p1.area());

        Area2 p2 = new Area2(10);
        System.out.println(p2.area());

        Area3 p3 = new Area3(10);
        System.out.println(p3.area());

    }
}
//(1) 在定义时直接赋值  public final int NUM = 10;
//     *  (2) 在构造方法中赋值
//     *  (3) 在代码块中赋值
class Area1{
    private final double PI =3.14;
    private double radius;

    public Area1(double radius) {
        this.radius = radius;
    }

    public double area(){
        return PI*radius*radius;
    }
}

class Area2{
    private final double PI;
    private double radius;

    public Area2(double radius) {
        this.radius = radius;
        PI = 3.14;
    }

    public double area(){
        return PI*radius*radius;
    }
}

class Area3{
    private final double PI;
    private double radius;

    {
        PI = 3.14;
    }

    public Area3(double radius) {
        // 调用代码块进行赋值
        this.radius = radius;
    }

    public double area(){
        return PI*radius*radius;
    }
}